//
//  GroupMingPianViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "GroupMingPianViewController.h"

@interface GroupMingPianViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *groupMingPianTF; // 小组名片

@end

@implementation GroupMingPianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 布局
    [self addMySubview];
}


// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"编辑小组名片";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(finish:)];
    rightBarItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 发布
- (void)finish:(UIBarButtonItem *)barItem
{

    NSLog(@"小组名片:%@， 长度:%ld", self.groupMingPianTF.text, self.groupMingPianTF.text.length);
    

    
    if (![self addZhengZe:self.groupMingPianTF.text] || self.groupMingPianTF.text.length < 2 || self.groupMingPianTF.text.length > 10) {
        [self addAlertNotSuit:@"需要输入2~10个汉字|字母|数字|下划线"];
    }
    
}

- (void)addAlertNotSuit:(NSString *)content
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:content preferredStyle:UIAlertControllerStyleAlert];
    // 修改标题颜色
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"提醒"];
    [title addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
    [title addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, 2)];
    [alertVC setValue:title forKey:@"attributedTitle"];
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
    }];
    [alertVC addAction:sureAction];
    [self presentViewController:alertVC animated:YES completion:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

- (BOOL)addZhengZe:(NSString *)string
{
    NSString *regex = @"^[a-zA-Z0-9\u4e00-\u9fa5,:_]+$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}

#pragma mark - 布局
- (void)addMySubview
{
    UILabel *grayLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15 * kAdjustParamater)];
    grayLine1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    [self.view addSubview:grayLine1];
    
    UILabel *grayLine2 = [[UILabel alloc] initWithFrame:CGRectMake(0, grayLine1.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine2.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine2];
    
    // 标题的白色背景
    UIView *titleWhiteBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, grayLine2.bottom, kScreenWidth, 50 * kAdjustParamater)];
    titleWhiteBackGroundView.backgroundColor = [UIColor whiteColor];
    titleWhiteBackGroundView.userInteractionEnabled = YES;
    [self.view addSubview:titleWhiteBackGroundView];
    
    // 更改的名片
    self.groupMingPianTF = [[UITextField alloc] initWithFrame:CGRectMake(15 * kAdjustParamater, 10 * kAdjustParamater, 345 * kAdjustParamater, 30 * kAdjustParamater)];
    self.groupMingPianTF.delegate = self;
    self.groupMingPianTF.text = @"我是群主:张国文";
    self.groupMingPianTF.textColor = [UIColor blackColor];
    self.groupMingPianTF.tintColor = kCyanColor;
    self.groupMingPianTF.textAlignment = NSTextAlignmentLeft;
    self.groupMingPianTF.font = [UIFont systemFontOfSize:k17Big];
    self.groupMingPianTF.clearButtonMode = UITextFieldViewModeAlways;
    [self.groupMingPianTF addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventAllEditingEvents];
    [titleWhiteBackGroundView addSubview:_groupMingPianTF];
    
    UILabel *grayLine3 = [[UILabel alloc] initWithFrame:CGRectMake(0, titleWhiteBackGroundView.bottom, kScreenWidth, 2 * kAdjustParamater)];
    grayLine3.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.view addSubview:grayLine3];
    
}

#pragma mark - text改变
- (void)textChange:(UITextField *)textField
{
    NSLog(@"text = %@", textField.text);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
